package com.xiaoi.user.xiaoi_demo;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xiaoi.user.xiaoi_sdk_asr.XRecognizerListener;
import com.xiaoi.user.xiaoi_sdk_asr.XSpeechRecognizer;
import com.xiaoi.user.xiaoi_sdk_common.SpeechListener;
import com.xiaoi.user.xiaoi_sdk_common.SpeechManger;
import com.xiaoi.user.xiaoi_sdk_reg.SpeechSemanticer;
import com.xiaoi.user.xiaoi_sdk_reg.XSemanticListener;
import com.xiaoi.user.xiaoi_sdk_tts.XTtsListener;
import com.xiaoi.user.xiaoi_sdk_tts.XTtser;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView content;
    Button start,start_UI;
    Button stream_reg,start_tts;

    class GUIShowInfoRunnable implements Runnable
    {
        private MainActivity _activity;
        private String _text;

        public GUIShowInfoRunnable(MainActivity activity, String text)
        {
            _activity = activity;
            _text = text;
        }

        @Override
        public void run()
        {
            showDebugInfoOnlyInMainThread(_text);
        }
    }

    public void showDebugInfo(String newinfo)
    {
        this.runOnUiThread(new GUIShowInfoRunnable(this, newinfo));
    }

    public void showDebugInfoOnlyInMainThread(String newInfo)
    {
        TextView textView = (TextView)findViewById(R.id.xiaoi_content);
        textView.setText(textView.getText().toString() + (textView.getText().toString().isEmpty() ? "" : "\n") + newInfo);

        final Layout layout = textView.getLayout();
        if(layout != null){
            int scrollDelta = layout.getLineBottom(textView.getLineCount() - 1)
                    - textView.getScrollY() - textView.getHeight();
            if(scrollDelta > 0)
                textView.scrollBy(0, scrollDelta);
        }
    }

    //录音初始化对象
    private SpeechManger speechManger;
    //语音识别对象
    public XSpeechRecognizer xSpeechRecognizer;
    //语义理解初始化对象
    SpeechSemanticer speechSemanticer;
    //语音合成初始化对象
    private XTtser xTtser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = (TextView)findViewById(R.id.xiaoi_content);
        content.setTextColor(Color.BLUE);
        start = (Button)findViewById(R.id.xiaoi_start);
        start_UI = (Button)findViewById(R.id.xiaoi_start_UI);
        stream_reg = (Button)findViewById(R.id.xiaoi_recognize_stream);
        start.setOnClickListener(this);
        start_UI.setOnClickListener(this);
        stream_reg.setOnClickListener(this);
        //初始化
        xSpeechRecognizer = new XSpeechRecognizer(this,xRecognizerListener);
        speechSemanticer = new SpeechSemanticer(xSemanticListener);
        xTtser = new XTtser(this,xTtsListener);
    }

    @Override
    public void onClick(View v) {
        if(v.equals(start)){
            //初始化
            speechManger = new SpeechManger(MainActivity.this,speechListener,"XIAOI",false);
            speechManger.startRecord();
        }else if(v.equals(start_UI)){
            speechManger = new SpeechManger(MainActivity.this,speechListener,"KDXF",true);
            speechManger.startRecord();
        }else if(v.equals(stream_reg)){

        }
    }

    /**
     * 科大讯飞语音识别参数设置
     */
    public void setRegkdxf(){
        HashMap<String, String> params = new HashMap<>();
        params.put("REG_FORMAT", "KDXF");
        params.put("KDXF_REG_RESULT_TYPE", "json");
        params.put("KDXF_REG_ENGINE_TYPE", "cloud");
        params.put("KDXF_REG_LANGUAGE", "zh_cn");
        xSpeechRecognizer.setParameter(params);
    }

    /**
     * xiaoi语音识别参数设置
     */
    public void setReg(){
        //xiaoi参数设置
        HashMap<String, String> params = new HashMap<>();
        params.put("REG_FORMAT", "XIAOI");
        params.put("XIAOI_REG_URL", "http://vcloud.xiaoi.com/recog");
        params.put("XIAOI_REG_KEY", "Bear1dj6N424431dj6N");
        params.put("XIAOI_REG_SECRET", "9d06crMEVDMg88v9q42W");
        //非必填参数
//        params.put("XIAOI_REG_USERID", "007");
//        params.put("XIAOI_REG_PLATFORM", "android");
//        params.put("XIAOI_REG_TIMEOUT", "5");
//        params.put("XIAOI_REG_TYPE", "xml");
        xSpeechRecognizer.setParameter(params);
    }

    /**
     * 语义理解参数设置
     */
    public void setSem(){
        //xiaoi参数设置
        HashMap<String, String> params = new HashMap<>();
        params.put("XIAOI_SEM_URL", "http://nlp.xiaoi.com/ask.do");
        params.put("XIAOI_SEM_SECRET", "g1PLtSFBadd3xNJYqulU");
        params.put("XIAOI_SEM_KEY", "XRg2TM9Ad1tT");
        //非必填参数
//        params.put("XIAOI_SEM_USERID", "007");
//        params.put("XIAOI_SEM_PLATFORM", "android");
//        params.put("XIAOI_SEM_TIMEOUT", "5");
//        params.put("XIAOI_SEM_TYPE", "json");
        speechSemanticer.setParameter(params);
    }

    public void setTtskdxf(){
        HashMap<String, String> params = new HashMap<>();
        params.put("TTS_type", "KDXF");
        params.put("KDXF_TTS_PITCH", "50");
        params.put("KDXF_TTS_VOLUME", "50");
        params.put("KDXF_TTS_TYPE", "cloud");
        params.put("KDXF_TTS_VOICE", "xiaoyan");
        params.put("KDXF_TTS_SPEED", "50");
        xTtser.setParameter(params);
    }

    public void setTts(){
        //xiaoi参数设置
        HashMap<String, String> params = new HashMap<>();
        params.put("TTS_type", "XIAOI");
        params.put("XIAOI_TTS_URL", "http://vcloud.xiaoi.com/synth.do");
        params.put("XIAOI_TTS_KEY", "Bear1dj6N424431dj6N");
        params.put("XIAOI_TTS_SECRET", "9d06crMEVDMg88v9q42W");
        //非必填参数
        params.put("XIAOI_TTS_USERID", "007");
        params.put("XIAOI_TTS_PLATFORM", "android");
        params.put("XIAOI_TTS_PLATFORM", "5");
        xTtser.setParameter(params);
    }

    /**
     * 录音初始化监听器
     */
    private SpeechListener speechListener = new SpeechListener() {
        @Override
        public void onStartRecord() {
            showDebugInfo("开始录音");
        }

        @Override
        public void onStopRecord() {
            showDebugInfo("停止录音");
            setRegkdxf();
            xSpeechRecognizer.doRecognize(speechManger.getCurrentAudioData());
        }

        @Override
        public void onCancelRecord() {

        }

        @Override
        public void onError(int code, String info) {
            if(code == 201){
                showDebugInfo("你没有说话！！");
            }else {
                showDebugInfo("错误"+info);
            }
        }

        @Override
        public void onStartPlay() {
            showDebugInfo("开始播放");
        }

        @Override
        public void onStopPlay() {
            showDebugInfo("停止播放");
        }

        @Override
        public void onAudioData(byte[] audioData) {
            showDebugInfo("录音数据长度"+audioData.length);
        }
    };

    /**
     *  语言识别初始化监听器
     */
    XRecognizerListener xRecognizerListener = new XRecognizerListener() {
        @Override
        public void onError(int code, String info) {
            showDebugInfo("错误"+info);
        }

        @Override
        public void onRecognizeStart() {
            showDebugInfo("开始识别");
        }

        @Override
        public void onRecognizeResult(int nCode, String strResult) {
            showDebugInfo("识别结果----"+strResult);
            if(strResult != null) {
                setSem();
                speechSemanticer.doAsk(strResult);
            }
        }
    };

    /**
     *  语义理解初始化监听器
     */
    private XSemanticListener xSemanticListener = new XSemanticListener() {
        @Override
        public void onSemanticStart() {
            showDebugInfo("语义理解开始");
        }

        @Override
        public void onSemanticResult(int nCode, String strResult) {
            showDebugInfo("语义理解结果----"+strResult);
            setTtskdxf();
            xTtser.startTts(strResult);
        }

        @Override
        public void onError(int code, String info) {
            showDebugInfo("语义识别错误信息----"+info);
        }
    };

    /**
     *  TTS初始化监听器
     */
    private XTtsListener xTtsListener = new XTtsListener() {
        @Override
        public void onTtsStart() {
            showDebugInfo("TTS开始");
        }

        @Override
        public void onTtsStop() {
            showDebugInfo("TTS结束");
        }

        @Override
        public void onTtsError(int code, String info) {
            showDebugInfo("TTS错误");
        }
    };


}
